//
//  AYMSymptomController.m
//  QuickCure
//
//  Created by kangkang on 16/5/6.
//  Copyright © 2016年 ayumi. All rights reserved.
//

#import "AYMSymptomController.h"

#import "AYMTumorModel.h"

@interface AYMSymptomController ()

@property(nonatomic,strong)NSIndexPath *firstPath;

@property(nonatomic,strong)NSIndexPath *secondPath;

@property(nonatomic,strong)NSString *firstString;

@property(nonatomic,strong)NSString *secondString;

@property(nonatomic,weak)UIBarButtonItem *button;

@property(nonatomic,strong)NSArray *sympArray;

@end

NSString *symID = @"Symcell";

@implementation AYMSymptomController

- (void)viewDidLoad {
    
    [super viewDidLoad];
   
    [self setupUI];
    
}

# pragma mark -
# pragma mark - setupUI
-(void)setupUI{
    
   self.navigationItem.title = @"并发症";
    
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:symID];
     [self loadData];
    
    UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStylePlain target:self action:@selector(didSureBtn)];
    
    
    
    
    item.tintColor = [UIColor darkGrayColor];
    
    item.enabled = NO;
   
    self.navigationItem.rightBarButtonItem = item;
    
    self.button = item;
    
    
}

# pragma mark -
# pragma mark - 抽取加载数据方法
-(void)loadData{
    
    
    NSArray *array = [AYMSQLiteManager queryTumorComplication_name];
    
    if (array.count != 0) {

        self.sympArray = array;
        
        [self.tableView reloadData];
        
    }else{
        
        
        NSString *urlStr = @"http://iosapi.itcast.cn/complicationList.json.php";
        
        NSDictionary *params = @{
                                 
                                 @"page":@1,
                                 @"page_size":@15,
                                 @"ci2_id":@3
                                 
                                 };
        
        [[AYMNetworkTool sharedManager] request:RequestMethodPost urlString:urlStr parameters:params finished:^(NSDictionary *responseObject, NSError *error) {
            
            if (error != nil) {
                
                Log(@"cuowu");
                
            }
            
            Log(@"%@",responseObject);
            
            NSArray *arr = responseObject[@"data"];
            
            NSMutableArray *tempArr = [NSMutableArray array];
            
            for (NSDictionary *dict in arr) {
                
                AYMTumorModel *model = [[AYMTumorModel alloc] init];
                
                [model yy_modelSetWithDictionary:dict];
                
                [tempArr addObject:model];
                
                [AYMSQLiteManager insertTumor_complication_nameWithModel:model];
            }
            
            self.sympArray = tempArr.copy;
            
            [self.tableView reloadData];
            
        }];
    }
    
}



# pragma mark -
# pragma mark - //点击确定按钮的方法
-(void)didSureBtn{
    
    NSString *first;
    
    if (self.firstString != nil) {
        
        first = self.firstString;
        
    }else{
        
        first = @"";
        
    }
    
    NSString *second;
    
    if (self.secondString != nil) {
        
        second = self.secondString;
        
    }else{
        
        second = @"";
        
    }
    
    NSString *str = [NSString stringWithFormat:@"[%@ %@]",first,second];
    
    if ([self.delegate respondsToSelector:@selector(symptomdelegateWith:)]) {
        
        [self.delegate symptomdelegateWith:str];
        
    }
    
    [self.navigationController popViewControllerAnimated:YES];
    
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.sympArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:symID forIndexPath:indexPath];
    
     AYMTumorModel *model = self.sympArray[indexPath.row];
    
    cell.textLabel.text = model.complication_name;
    
    if (indexPath == self.firstPath) {
      
        cell.imageView.image = [UIImage imageNamed:@"illness_rb_img_sel.png"];
 
        self.firstString = cell.textLabel.text;
       
    }else if (indexPath == self.secondPath){
        
        cell.imageView.image = [UIImage imageNamed:@"illness_rb_img_sel.png"];
        
        self.secondString = cell.textLabel.text;
        
    }else{
        
        cell.imageView.image = [UIImage imageNamed:@"illness_rb_img_nor.png"];
        
    }


    cell.selectionStyle = UITableViewCellSelectionStyleNone;
  
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.secondPath == indexPath) {
        
        self.secondPath = nil;
        
        self.secondString = nil;
 
        if (self.firstPath == nil && self.secondPath == nil) {
            
                        self.button.enabled = NO;
            
                        self.button.tintColor = [UIColor darkGrayColor];
            
                }
        

        [tableView reloadData];
        
        return;
        
    }else if (self.firstPath == indexPath) {
        
        self.firstPath = nil;
        
        self.firstString = nil;
        
        if (self.firstPath == nil && self.secondPath == nil) {
            
            self.button.enabled = NO;
            
            self.button.tintColor = [UIColor darkGrayColor];
            
        }

        [tableView reloadData];
        
        return;
        
    }
    
    if (self.firstPath != nil && self.secondPath != nil) {
        
        self.firstPath = self.secondPath;
        
        self.secondPath = indexPath;
  
    }

    if (self.firstPath == nil) {
        
        self.firstPath = indexPath;
        
    }else{
        
        self.secondPath = indexPath;
        
    }
    
    if (self.firstPath != nil || self.secondPath != nil) {
        
        self.button.enabled = YES;
        
       self.button.tintColor = [UIColor whiteColor];
        
    }
    
    [tableView reloadData];

}

@end
